package com.ruoyi.web.controller.guomao;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IGmElecCostService;
import com.ruoyi.system.service.ISysFloorService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 国贸电费管理Controller
 *
 * @author xty
 * @date 2021-12-15
 */
@Controller
@RequestMapping("/guomao/elecCostManagement")
public class GmElecCostController extends BaseController
{
    private String prefix = "propertys/guomao/elecCostManagement";


    @Autowired
    private IGmElecCostService elecCostService;

//    楼层信息
    @Autowired
    private ISysFloorService floorInfoService;

    /**
     * 楼层信息查询
     */
//    @RequiresPermissions("property:tenant:view")
    @GetMapping()
    public String elecCost(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/main";
    }


    /**
     * 查询租户管理列表
     */
//    @RequiresPermissions("property:tenant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GmElecCostInfo elecCostInfo)
    {
        startPage();
//        List<GuomaoTenantInfo> list = tenantInfoService.selectAllGuomaoTenantInfo();//selectTenantInfoList(tenantInfo)
        List<GmElecCostInfo> list = elecCostService.selectGmElecCostInfoList(elecCostInfo);
        return getDataTable(list);
    }

    /**
     * 新增租户管理
     * 页面跳转
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/add";
    }

    /**
     * 新增保存租户管理
     * 保存新增
     */
//    @RequiresPermissions("property:tenant:add")
    @Log(title = "电费管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GmElecCostInfo elecCostInfo)
    {
        return toAjax(elecCostService.insertGmElecCostInfo(elecCostInfo));
    }



    /**
     * 修改电费管理
     * 页面跳转
     */
    @GetMapping("/edit/{elecCostId}")
    public String edit(@PathVariable("elecCostId") String elecCostId, ModelMap modelMap)
    {
        GmElecCostInfo elecCostInfo = elecCostService.selectGmElecCostInfoById(elecCostId);
        modelMap.put("elecCostInfo", elecCostInfo);
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/edit";
    }

    /**
     * 修改保存电费管理
     * 保存修改
     */
//    @RequiresPermissions("property:tenant:edit")
    @Log(title = "电费管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GmElecCostInfo elecCostInfo)
    {
        return toAjax(elecCostService.updateGmElecCostInfo(elecCostInfo));
    }

    /**
     * 删除租户管理
     */
//    @RequiresPermissions("property:tenant:remove")
    @Log(title = "租户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(elecCostService.deleteGmElecCostByIds(ids));
    }


    /**
     * 导入电费列表
     * */
    @Log(title = "电费管理", businessType = BusinessType.IMPORT)
//    @RequiresPermissions("property:elecCost:import")
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GmElecCostInfo> util = new ExcelUtil<>(GmElecCostInfo.class);
        List<GmElecCostInfo> lists = util.importExcel(file.getInputStream());
        SysUser sysUser = ShiroUtils.getSysUser();
        //校验数据
        String res = elecCostService.validateDatas(lists);
        if (StringUtils.isNotBlank(res)){
            return AjaxResult.error(res);
        }
//        仅当没有错误信息时才会执行导入操作
        String message =elecCostService.importRelations(lists,updateSupport,sysUser);

        return AjaxResult.success(message);
    }


    /**
     * 导入中的下载模板
     * */
//    @RequiresPermissions("property:elecCost:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<GmElecCostExcel> util = new ExcelUtil<GmElecCostExcel>(GmElecCostExcel.class);
        return util.importTemplateExcel("电费信息");
    }



    /**
     * 导出电费抄表信息列表
     */
//    @RequiresPermissions("property:tenant:export")
    @Log(title = "抄表导出", businessType = BusinessType.EXPORT)
    @PostMapping("/allExport")
    @ResponseBody
    public AjaxResult allExport(@Param("elecMonth")String elecMonth)
    {
//        String[] tanantIds = Convert.toStrArray(ids);
        List<GmElecCostInfo> list = elecCostService.selectGmElecCostByMonth(elecMonth);
        ExcelUtil<GmElecCostInfo> util = new ExcelUtil<GmElecCostInfo>(GmElecCostInfo.class);
        return util.exportExcel(list, elecMonth+"电表信息");
    }



    /**
     * 导出电费单位信息列表
     */
//    @RequiresPermissions("property:tenant:export")
    @Log(title = "单位导出", businessType = BusinessType.EXPORT)
    @PostMapping("/leaderExport")
    @ResponseBody
    public AjaxResult leaderExport(@Param("elecMonth")String elecMonth)
    {
//        String[] tanantIds = Convert.toStrArray(ids);
        List<GmElecCostInfo> list = elecCostService.selectGmElecCostByMonth(elecMonth);
        List<GmElecExportInfo> exportInfos = new ArrayList<>();
        Set nameSet = new HashSet();
//        统计公司信息
        for (int i = 0;i<list.size();i++)
        {
            nameSet.add(list.get(i).getCompanyName());
        }
        List<String> nameList = new ArrayList(nameSet);
        for (int i = 0;i<nameList.size();i++){
            exportInfos.add(i,new GmElecExportInfo());
            List<GmElecCostInfo> listForName = elecCostService.selectGmElecCostByCName(nameList.get(i));
            exportInfos.get(i).setCompanyName(nameList.get(i));
            exportInfos.get(i).setProjectName(listForName.get(0).getProjectName());
            exportInfos.get(i).setElecMonth(listForName.get(0).getElecMonth());
            double degree = 0,elecTotalPrice = 0;
            for (int j = 0;j<listForName.size();j++){
                degree += listForName.get(j).getDegree();
                elecTotalPrice += listForName.get(j).getElecTotalPrice();
            }
            exportInfos.get(i).setDegree(degree);
            exportInfos.get(i).setElecTotalPrice(elecTotalPrice);
        }
        ExcelUtil<GmElecExportInfo> util = new ExcelUtil<GmElecExportInfo>(GmElecExportInfo.class);
        return util.exportExcel(exportInfos, "单位"+elecMonth+"电表");
    }


}
